<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never">
      <el-form :inline="true" :model="dataForm">
        <el-form-item label="公司名称">
          <el-autocomplete
            clearable
            v-model="dataForm.companyName"
            :fetch-suggestions="querySearch"
            placeholder="请输入公司名称"
            :trigger-on-focus="false"
            @select="handleSelect"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" shadow="never">
      <!-- 数据展示 -->
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#f0f3fa',textAlign:'center'}"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        :cell-style="{textAlign:'center'}"
      >
        <!-- <el-table-column prop="date" label="公司名称"></el-table-column> -->
        <el-table-column prop="name" label="用水类型"></el-table-column>
        <el-table-column prop="waterMonth" label="预警阈值(吨/月)"></el-table-column>
        <el-table-column prop="waterSeason" label="预警阈值(吨/季度)"></el-table-column>
        <el-table-column prop="waterHalf" label="预警阈值(吨/半年)"></el-table-column>
        <el-table-column prop="waterYear" label="预警阈值(吨/年)"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" content="详情">
              <i class="el-icon-info" @click="handleEdit('info',scope.row)"></i>
            </el-tooltip>
            <el-tooltip class="item" content="设置预警阀值">
              <i class="el-icon-setting" @click="handleEdit('edit',scope.row)"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="dataForm.page"
        :page-sizes="[10, 20, 40, 60]"
        :page-size="dataForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 阈值设置 -->
    <thresholdSet ref="thresholdSet" @updata="onSubmit"></thresholdSet>
  </div>
</template>

<script>
import thresholdSet from './component/thresholdSet.vue'
import { getListWarning } from '@/api/other'
export default {
  // 组件注册
  components: {
    thresholdSet,
  },
  data() {
    return {
      dataForm: {
        companyId: '',
        companyName: '',
        page: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 0,
    }
  },
  mounted() {
    this.getListWarningReq()
  },
  methods: {
    // querySearch()  该方法在混入中
    getListWarningReq() {
      getListWarning(this.dataForm).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        const { total, list } = data
        this.total = total
        this.tableData = list
      })
    },
    onSubmit() {
      this.dataForm.page = 1
      this.getListWarningReq()
    },
    handleEdit(type, item) {
      this.$nextTick((_) => {
        this.$refs.thresholdSet.init(type, item)
      })
    },
    handleDelete(index, row) {
      console.log('删除', index, row)
    },
    handleSelect(item) {
      this.dataForm.companyId = item.id
    },
    // 监听pageSize改变事件
    handleSizeChange(newSize) {
      this.dataForm.pageSize = newSize
      this.getListWarningReq()
    },
    // 监听 页面值 改变的事件
    handleCurrentChange(newPage) {
      this.dataForm.page = newPage
      this.getListWarningReq()
    },
  },
}
</script>

<style lang='scss' scoped>
</style>
